package com.justspeak;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ViewSwitcher.ViewFactory;

public class SystemSetActivity extends Activity {

    private Gallery gallery; // 选择的相册
    private ImageSwitcher imageswitch;// 显示的相框
    private Button btn_updataImager;
    private ImageView img_view;
    private int selectImager; // 选择中图片的索引
    private AlertDialog.Builder builder;
    private AlertDialog alertdialog = null;

    private int[] images = { R.drawable.icon,
	    R.drawable.add_image1, // 所有图片集合
	    R.drawable.add_image2, R.drawable.add_image3,
	    R.drawable.add_image4, R.drawable.add_image5, R.drawable.add_image6 };

    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.systemset);
	initView();
	btn_updataImager.setOnClickListener(new UpdateImagerLisener(this));
    }

    private void initView() {
	btn_updataImager = (Button) findViewById(R.id.myUpdatePhoto_btn);
	img_view = (ImageView) findViewById(R.id.myImageView);
    }

    class UpdateImagerLisener implements OnClickListener {
	private Context context;

	private UpdateImagerLisener(Context context) {
	    this.context = context;
	}

	public void onClick(View v) {
	    initDialog();
	    alertdialog.show();
	}

	private void initDialog() { // 初始化对话框
	    LayoutInflater inflater = LayoutInflater.from(context);
	    builder = new AlertDialog.Builder(context);
	    builder.setTitle("请选择图像");
	    builder.setPositiveButton("确定", // 在dialog中设置一个积极按钮
		    new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
			    img_view.setImageResource(images[selectImager
				    % images.length]);
			}
		    });
	    builder.setNegativeButton("返回", // 在dialog中设置一个消极按钮
		    new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
			    alertdialog.dismiss();
			}
		    });
	    View inflaterView = inflater.inflate(R.layout.showimager, null);
	    gallery = (Gallery) inflaterView.findViewById(R.id.mygallery);
	    imageswitch = (ImageSwitcher) inflaterView
		    .findViewById(R.id.mysimageswitcher);

	    gallery.setAdapter(new MyImageAdepter(context));
	    gallery.setSelection(images.length / 2);
	    gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

		public void onItemSelected(AdapterView<?> arg0, View arg1,
			int position, long arg3) {
		    selectImager = position;
		    imageswitch.setImageResource(images[selectImager // 在相框中显示选择的图片
			    % images.length]);
		}

		public void onNothingSelected(AdapterView<?> arg0) {

		}

	    });

	    imageswitch.setFactory(new MyImageFactory(context));// 在imageswith中设置个image工厂供它显示
	    builder.setView(inflaterView);
	    alertdialog = builder.create();
	}

    }

    class MyImageFactory implements ViewFactory {
	private Context context = null;

	private MyImageFactory(Context context) {
	    this.context = context;
	}

	public View makeView() { // 生成一个可以改变的ImageView
	    ImageView imagerview = new ImageView(context);
	    imagerview.setImageResource(images[selectImager % images.length]);
	    imagerview
		    .setLayoutParams(new ImageSwitcher.LayoutParams(100, 100));
	    return imagerview;
	}
    }

    class MyImageAdepter extends BaseAdapter { // 设置一个适配器类 提供个Gallery的显示图片
	private Context context;

	public MyImageAdepter(Context context) {
	    this.context = context;
	}

	public int getCount() {
	    return Integer.MAX_VALUE;// 设置最大值， 以便可以生成最最大次的view在Gallery中避免不连续出现（可
	    // 执行2147483647次）
	}

	public Object getItem(int position) {
	    return null;
	}

	public long getItemId(int position) {
	    return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
	    ImageView view = new ImageView(context);
	    view.setImageResource(images[position % images.length]);// 可循环显示相册
	    view.setAdjustViewBounds(true);
	    view.setLayoutParams(new Gallery.LayoutParams(100, 100));// 设置在Gallery的图片大小参数
	    view.setPadding(15, 10, 15, 10);
	    return view;
	}
    }

}
